﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RenameTool.PartialStringParser
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;


namespace DnProcessSimulateCommands.RenameTool;

internal class PartialStringParser
{
  private PartialStringParser.PartialString[] strings;
  private List<string> listOfAttributes;
  private List<Dictionary<string, string>> attributesValues;

  public RuleMessage Parse(string replaceString)
  {
    List<PartialStringParser.PartialString> partialStringList = new List<PartialStringParser.PartialString>();
    if (replaceString == null)
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_PARSER_NULL"));
    if (replaceString.Length > 1)
    {
      int startIndex1 = 0;
      int startIndex2 = 0;
      char[] chArray = new char[2]{ '[', '{' };
      int num;
      for (char[] anyOf = chArray; replaceString.Length > startIndex1 && (num = replaceString.IndexOfAny(anyOf, startIndex1)) >= 0; startIndex1 = startIndex2 + 1)
      {
        char ch = (char) ((uint) replaceString[num] + 2U);
        startIndex2 = replaceString.IndexOf(ch, num);
        if (startIndex2 == -1)
          return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_PARSER_CLOSING_BRACKET"));
        string part1 = replaceString.Substring(startIndex1, num - startIndex1);
        if (part1.Length > 0)
        {
          PartialStringParser.PartialString partialString = new PartialStringParser.PartialString(part1);
          partialStringList.Add(partialString);
        }
        string part2 = replaceString.Substring(num + 1, startIndex2 - num - 1);
        PartialStringParser.PartialString partialString1;
        if (ch == ']')
        {
          string[] strArray = part2.Split(',');
          if (strArray.Length > 3 || strArray.Length < 2)
            return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_PARSER_WRONG_FORMAT"));
          int result1;
          int result2;
          if (strArray.Length == 3)
          {
            if (!int.TryParse(strArray[1].Trim(), out result1))
              return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_PARSER_WRONG_STEP"));
            if (!int.TryParse(strArray[2].Trim(), out result2))
              return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_WRONG_DIGITS_LIMIT"));
          }
          else
          {
            result1 = 1;
            if (!int.TryParse(strArray[1].Trim(), out result2))
              return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_WRONG_DIGITS_LIMIT"));
          }
          result2 = result2 != 0 ? 0 : (strArray[0].Trim()[0] == '-' ? strArray[0].Trim().Length - 1 : strArray[0].Trim().Length);
          int result3;
          if (int.TryParse(strArray[0].Trim(), out result3))
          {
            partialString1 = new PartialStringParser.PartialString(result3, result1, result2);
          }
          else
          {
            if (strArray[0].Trim().Length != 1)
              return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_PARSER_WRONG_ENUM"));
            char part3 = strArray[0].Trim()[0];
            if ((part3 < 'A' || part3 > 'Z') && (part3 < 'a' || part3 > 'z'))
              return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_PARSER_WRONG_ENUM"));
            partialString1 = new PartialStringParser.PartialString(part3, result1);
          }
        }
        else
        {
          partialString1 = new PartialStringParser.PartialString(part2, true);
          if (this.listOfAttributes == null)
            this.listOfAttributes = new List<string>();
          this.listOfAttributes.Add(part2);
        }
        partialStringList.Add(partialString1);
      }
      if (startIndex2 < replaceString.Length - 1)
      {
        if (startIndex2 > 0)
          ++startIndex2;
        PartialStringParser.PartialString partialString = new PartialStringParser.PartialString(replaceString.Substring(startIndex2));
        partialStringList.Add(partialString);
      }
    }
    else
    {
      PartialStringParser.PartialString partialString = new PartialStringParser.PartialString(replaceString);
      partialStringList.Add(partialString);
    }
    this.strings = partialStringList.ToArray();
    return new RuleMessage(true);
  }

  public List<string> GetAttributes() => this.listOfAttributes;

  public void SetAttributesValues(List<Dictionary<string, string>> attributesValues)
  {
    this.attributesValues = attributesValues;
  }

  public string GetNextString(int index)
  {
    string nextString = "";
    foreach (PartialStringParser.PartialString partialString in this.strings)
    {
      if (partialString.IsAttribute)
      {
        if (this.attributesValues != null)
        {
          Dictionary<string, string> attributesValue = this.attributesValues[index];
          foreach (string key in attributesValue.Keys)
          {
            if (key == partialString.Part)
            {
              nextString += CApRenameToolManager.ReplaceNewLineWithSpace(attributesValue[key]);
              break;
            }
          }
        }
      }
      else
        nextString += partialString.Part;
    }
    return nextString;
  }

  public static string GetEnumeratorString(string enumerator, string step, bool limited)
  {
    if (enumerator.Length == 0)
      return "";
    return step.Length == 0 ? $"[{enumerator.Trim()},{(limited ? (object) 0.ToString() : (object) 1.ToString())}]" : $"[{enumerator.Trim()},{step.Trim()},{(limited ? (object) 0.ToString() : (object) 1.ToString())}]";
  }

  private class PartialString
  {
    private string part;
    private char charEnum;
    private int intEnum;
    private int step;
    private PartialStringParser.PartialString.EnumType enumType;
    private int digitsLimit;

    public PartialString(string part)
    {
      this.enumType = PartialStringParser.PartialString.EnumType.String;
      this.part = part;
    }

    public PartialString(int part, int step, int dl)
    {
      this.enumType = PartialStringParser.PartialString.EnumType.Number;
      this.intEnum = part;
      this.step = step;
      this.digitsLimit = dl;
    }

    public PartialString(char part, int step)
    {
      this.enumType = PartialStringParser.PartialString.EnumType.Char;
      this.charEnum = part;
      this.step = step;
    }

    public PartialString(string part, bool isAttribute)
    {
      this.enumType = !isAttribute ? PartialStringParser.PartialString.EnumType.String : PartialStringParser.PartialString.EnumType.Attribute;
      this.part = part;
    }

    internal string Part
    {
      get
      {
        switch (this.enumType)
        {
          case PartialStringParser.PartialString.EnumType.String:
            return this.part;
          case PartialStringParser.PartialString.EnumType.Char:
            this.part = this.charEnum.ToString();
            this.charEnum += (char) this.step;
            if (this.part[0] >= 'A' && this.part[0] <= 'Z')
            {
              if (this.charEnum < 'A')
                this.charEnum = 'A';
              else if (this.charEnum > 'Z')
                this.charEnum = 'Z';
            }
            else if (this.part[0] >= 'a' && this.part[0] <= 'z')
            {
              if (this.charEnum < 'a')
                this.charEnum = 'a';
              else if (this.charEnum > 'z')
                this.charEnum = 'z';
            }
            return this.part;
          case PartialStringParser.PartialString.EnumType.Number:
            return this.GetNextNumber();
          case PartialStringParser.PartialString.EnumType.Attribute:
            return this.part;
          default:
            throw new Exception();
        }
      }
    }

    internal bool IsAttribute
    {
      get => this.enumType == PartialStringParser.PartialString.EnumType.Attribute;
    }

    private string GetNextNumber()
    {
      this.part = this.intEnum.ToString();
      int startIndex = this.intEnum >= 0 ? 0 : 1;
      if (this.digitsLimit != 0)
      {
        int num = Math.Abs(this.part.Substring(startIndex).Length - this.digitsLimit);
        for (int index = 0; index < num; ++index)
          this.part = this.part.Insert(startIndex, "0");
      }
      this.intEnum += this.step;
      if (this.digitsLimit != 0)
        this.intEnum %= (int) Math.Pow(10.0, (double) this.digitsLimit);
      return this.part;
    }

    private enum EnumType
    {
      String,
      Char,
      Number,
      Attribute,
    }
  }
}
